package com.tedu.charging.service.cost;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tedu.charging.service.cost.dao.CostRuleMapper;
import com.tedu.charging.common.pojo.bo.ChargeData;
import com.tedu.charging.service.cost.pojo.po.ChargingCostRule;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@Component
@Slf4j
public class CostInit {
    //保存每个订单充电数据
    public static ConcurrentHashMap<String, ChargeData> billChargingDataConcurrentHashMap = new ConcurrentHashMap<>();

    //保存每个充电站的计费规则
    public static ConcurrentHashMap<String, List<ChargingCostRule>> stationCostConcurrentHashMap = new ConcurrentHashMap<>();

    @Autowired
    CostRuleMapper costRuleMapper;

    //项目启动时执行init方法
    @PostConstruct
    public void init() {
        //读取所有充电站
        List<ChargingCostRule> stationIdList = costRuleMapper.selectStation();
        for (ChargingCostRule chargingCostRule : stationIdList) {
            Integer stationId = chargingCostRule.getStationId();
            //查询慢充计费规则
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("station_id", stationId);
            queryWrapper.eq("gun_type", 1);//1是慢充
            List<ChargingCostRule> list = costRuleMapper.selectList(queryWrapper);
            stationCostConcurrentHashMap.put(stationId + "-1", list);

            //查询快充计费规则
            queryWrapper.eq("station_id", stationId);
            queryWrapper.eq("gun_type", 2);//2是快充
            list = costRuleMapper.selectList(queryWrapper);
            stationCostConcurrentHashMap.put(stationId + "-2", list);
        }
        log.debug("计费信息", stationCostConcurrentHashMap);
    }
}
